package com.jdcjk.base.view;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;

import com.jdcjk.R;
import com.mvp.presenter.impl.MvpBasePresenter;
import com.mvp.view.impl.MvpActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dong on 2016/12/1.
 * Activity基础实现类
 */
public abstract class BaseActivity<P extends MvpBasePresenter> extends MvpActivity<P> {
    private final String TAG = BaseActivity.class.getSimpleName();
    private BaseFragment mCurrentFragment;
    private FragmentManager fragmentManager;
    private List<Integer> fragmentIndexList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        if (fragmentManager == null) {
            fragmentManager = getSupportFragmentManager();
        }
        if (fragmentIndexList == null) {
            fragmentIndexList = new ArrayList<>();
        }
        initContentView(savedInstanceState);
        initData();
    }



    /**
     * Fragment初始化
     *
     * @param fragment 初始化的Fragment
     * @param resId    装载Fragment的view Id
     * @param index    Fragment的标识index
     */
    protected void addFragmentContent(BaseFragment fragment, int resId, int index) {
        if (fragmentManager.getFragments() == null) {
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.add(resId, fragment, String.valueOf(index));
            fragmentTransaction.commit();
            mCurrentFragment = fragment;
            fragmentIndexList.add(index);
        }
    }

    /**
     * Fragment切换
     *
     * @param toFragment   将要切换到的Fragment
     * @param resId        装载Fragment的view Id
     * @param index        Fragment的标识index
     * @param useAnim      判断Fragment切换是否采用动画
     * @param useBackStack 判断Fragment是否使用回退栈
     */
    protected void switchDiffFragmentContent(BaseFragment toFragment, int resId, int index, boolean useAnim, boolean useBackStack) {
        if (null == mCurrentFragment || null == toFragment) {
            return;
        }
        // 判断Fragment标识是否重复
        if (!mCurrentFragment.getTag().equals(String.valueOf(index))) {
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            // 设置跳转动画
            if (useAnim) {
                fragmentTransaction.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out, R.anim.push_right_out, R.anim.push_right_in);
            }
            // 先判断是否添加过
            if (!toFragment.isAdded()) {
                // 隐藏当前fragment,add下一个fragment
                fragmentTransaction.hide(mCurrentFragment);
                fragmentTransaction.add(resId, toFragment, String.valueOf(index));
                if (useBackStack) {
                    fragmentTransaction.addToBackStack(String.valueOf(index));
                }
                fragmentTransaction.commit();
                Log.d("TAG", "fragment is not add");
            } else {
                // 隐藏当前fragment，show下一个fragment
                fragmentTransaction.hide(mCurrentFragment);
                fragmentTransaction.show(toFragment);
                if (useBackStack) {
                    fragmentTransaction.addToBackStack(String.valueOf(index));
                }
                fragmentTransaction.commit();
                Log.d("TAG", "fragment is added");
            }
            // 刷新当前fragment
            mCurrentFragment = toFragment;
            if (useBackStack) {
                // 添加fragmentIndex
                fragmentIndexList.add(index);
            }
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        // 判断回退栈的大小是否大于1
        if (fragmentIndexList.size() > 1) {
            // 移除Fragment
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.remove(fragmentManager.getFragments().get(fragmentIndexList.size() - 1));
            fragmentTransaction.commit();
            // 移除List集合最后一个fragmentIndex
            fragmentIndexList.remove(fragmentIndexList.size() - 1);
            // 修改当前的Fragment
            mCurrentFragment = (BaseFragment) fragmentManager.getFragments().get(fragmentIndexList.size() - 1);
        }
    }

    @Override
    public P bindPresenter() {
        return null;
    }

    public abstract int getLayoutId();

    public abstract void initContentView(Bundle savedInstanceState);

    public void initData() {
    }

    public void refreshView() {
    }

}
